package com.hyx.server.service;

import com.hyx.server.domain.CourseContentFile;
import com.hyx.server.domain.CourseContentFileExample;
import com.hyx.server.dto.CourseContentFileDto;
import com.hyx.server.mapper.CourseContentFileMapper;
import com.hyx.server.util.CopyUtil;
import com.hyx.server.util.UuidUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;


@Service
public class CourseContentFileService {

    @Resource
    private CourseContentFileMapper courseContentFileMapper;

    /**
     * 列表查询
     * @param courseId
     */
    public List<CourseContentFileDto> list(String courseId){
        CourseContentFileExample courseContentFileExample = new CourseContentFileExample();
        CourseContentFileExample.Criteria criteria = courseContentFileExample.createCriteria();
        criteria.andCourseIdEqualTo(courseId);
        List<CourseContentFile> fileList = courseContentFileMapper.selectByExample(courseContentFileExample);
        return CopyUtil.copyList(fileList, CourseContentFileDto.class);
    };

    /**
     * 保存，id有值时更新，无值时新增
     * @param courseContentFileDto
     */
    public void save(CourseContentFileDto courseContentFileDto){
        CourseContentFile courseContentFile = CopyUtil.copy(courseContentFileDto,CourseContentFile.class);
        if (StringUtils.isEmpty(courseContentFileDto.getId())){
                this.insert(courseContentFile);
        }else{
                this.update(courseContentFile);
        }
    };

    /**
     * 新增
     * @param courseContentFile
     */
    private void insert(CourseContentFile courseContentFile){
        courseContentFile.setId(UuidUtil.getShortUuid());
        courseContentFileMapper.insert(courseContentFile);
    };

    /**
     * 更新
     * @param courseContentFile
     */
    private void update(CourseContentFile courseContentFile){
        courseContentFileMapper.updateByPrimaryKey(courseContentFile);
    };

    /**
     * 删除
     * @param id
     */
    public void delete(String id){
        courseContentFileMapper.deleteByPrimaryKey(id);
    };
}